package cn.netjava.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.netjava.dbutil.Conn2DB;
import cn.netjava.pojo.Msg;

/**
 * the class is use for dealing with message
 * @author Administrator
 *
 */
public class MsgDao {
	
	public static MsgDao mdao;
	
	/*make PhotoDao pravate*/
	private MsgDao(){}
	
	/*obtain a PhotoDao object*/
	public static MsgDao getMsgDao(){
		if(null==mdao){
			mdao = new MsgDao();
		}
		return mdao;
	}
	
	/**
	 * to add a message 
	 * @param msgContent
	 * @param userID
	 * @param toUserID
	 * @return
	 */
	public boolean addMsg(String msgContent,int userID,int toUserID){
		String sql="insert into tbl_msgs(msgcontent,ins_uid,ins_touid,ins_dt)values(?,?,?,?)";
		try{
			java.sql.Connection conn =Conn2DB.getConn();
			java.sql.PreparedStatement pstmt =conn.prepareStatement(sql);
			
			pstmt.setString(1, msgContent);
			pstmt.setInt(2, userID);
			pstmt.setInt(3, toUserID);
			/*obtain the system time*/
			java.sql.Timestamp timeStamp =new java.sql.Timestamp(System.currentTimeMillis());
			String ins_td =timeStamp.toString().substring(0,timeStamp.toString().lastIndexOf("."));
			pstmt.setString(4, ins_td); 
			
			pstmt.executeUpdate();			
			pstmt.close();						
			return true;
		}catch(Exception ef){
			ef.printStackTrace();
		}
		return false;		
	}
	
	/**
	 * to get the user's all message
	 * @param toUserID
	 * @return
	 */
	public List<Msg> getUserMsgFromUserID(int toUserID){
		List<Msg> msgList =new ArrayList<Msg>();
		String sql="select * from tbl_msgs where ins_touid=?";
		try{
			java.sql.Connection conn =Conn2DB.getConn();
			java.sql.PreparedStatement pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, toUserID);
			
			ResultSet rs =pstmt.executeQuery();
			while(rs.next()){
				Msg msg =new Msg();
				
				msg.setId(rs.getInt("id"));
				msg.setMsgContent(rs.getString("msgcontent"));
				UserDao udao =UserDao.getUserDao();
				msg.setUser(udao.getUserFromID(rs.getInt("ins_uid")));
				msg.setToUser(udao.getUserFromID(rs.getInt("ins_touid")));
				msg.setInt_dt(rs.getString("ins_dt"));
				
				msgList.add(msg);
			}
			pstmt.close();	
			rs.close();
		}catch(Exception ef){
			ef.printStackTrace();
		}
		return msgList;
	}
	
	/**
	 * to get the Msg according to the message ID
	 * @param msgID
	 * @return
	 */
	public Msg getMsgFromMsgID(int msgID){
		Msg msg =new Msg();
		String sql="select * from tbl_msgs where id=?";
		try{
			java.sql.Connection conn =Conn2DB.getConn();
			java.sql.PreparedStatement pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, msgID);
			
			ResultSet rs =pstmt.executeQuery();
			if(rs.next()){
				msg.setId(rs.getInt("id"));
				msg.setMsgContent(rs.getString("msgcontent"));
				UserDao udao =UserDao.getUserDao();
				msg.setUser(udao.getUserFromID(rs.getInt("ins_uid")));
				msg.setToUser(udao.getUserFromID(rs.getInt("ins_touid")));
				msg.setInt_dt(rs.getString("ins_dt"));				
			}
			pstmt.close();	
			rs.close();
		}catch(Exception ef){
			ef.printStackTrace();
		}	
		return msg;
	}

}
